package com.fare.taluo.core.domain.analyse.alert.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.dayatang.domain.QuerySettings;
import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.analyse.alert.FreightPriceAnalyseAlertConfigEntity;
import com.fare.taluo.core.domain.analyse.alert.FreightPriceAnalyseAlertConfigRepository;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringAndDateAndFileUtil;
@Repository
public class FreightPriceAnalyseAlertConfigRepositoryImpl 
extends EntityRepositoryHibernate 
implements FreightPriceAnalyseAlertConfigRepository{

	@Override
	public List<FreightPriceAnalyseAlertConfigEntity> getAllAnalyseAlert(Long userID) {
		return this.find("from FreightPriceAnalyseAlertConfigEntity f where f.flgDeleted = 0 and f.creator='"+userID+"'",new ArrayList().toArray(),FreightPriceAnalyseAlertConfigEntity.class);
	}

	@Override
	public List<FreightPriceAnalyseAlertConfigEntity> getAllAnalyseAlertByC(
			String alertType ,Date createTime,Date endCreateTime ,Long userId) {
		String hql = "from FreightPriceAnalyseAlertConfigEntity f where f.flgDeleted = 0 ";
		List<Object> list = new ArrayList<Object>();
		
		if(StringUtils.isNotBlank(alertType)&& !alertType.equals("all")){
			hql+="and f.alertType = ? ";
			list.add(Integer.parseInt(alertType));
		}
		if(createTime != null && endCreateTime !=null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(endCreateTime);
			calendar.add(Calendar.DATE, 1);
			//calendar.set(Calendar., value)
			hql+="and f.createTime >= ? and f.createTime <= ? ";
			list.add(createTime);
			list.add(calendar.getTime());
		}
		if(userId != null){
			hql+="and f.creator = ? ";
			list.add(userId);
		}
		return this.find(hql,list.toArray(),FreightPriceAnalyseAlertConfigEntity.class);
	}

	@Override
	public FreightPriceAnalyseAlertConfigEntity getConfigByLine(
			String hbaseKey, String fromCityCode, String toCityCode, Long userId) {
 		return this.getSingleResult(QuerySettings.create(FreightPriceAnalyseAlertConfigEntity.class).eq("carrierKey", hbaseKey)
				.eq("fromCity", fromCityCode).eq("toCity", toCityCode).eq("toCity", toCityCode).eq("creator", userId));
	}
    
	public FreightPriceAnalyseAlertConfigEntity getCabinChangeConfig(String flightNo,String flightDate,Long userId){
		String hql = "from FreightPriceAnalyseAlertConfigEntity f where f.flgDeleted = 0 ";
		List<Object> list = new ArrayList<Object>();
		
		if(StringUtils.isNotBlank(flightNo)){
			hql+="and f.flightNo = ? ";
			list.add(flightNo);
		}
		if(flightDate != null ){
			/*Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			calendar.add(Calendar.DATE, 1);*/
			hql+="and f.flightDate = ?  ";
			try {
				list.add(DateUtils.parseDate(flightDate,"yyyy-MM-dd"));
			} catch (ParseException e) {
				
				e.printStackTrace();
			}
			
		}
		if(userId != null){
			hql+="and f.creator = ? ";
			list.add(userId);
		}
		
		
		System.out.println("查询hql语句："+hql.toString());
		
			List<FreightPriceAnalyseAlertConfigEntity> entityList = this.find(hql,list.toArray(),FreightPriceAnalyseAlertConfigEntity.class);
		
		if(entityList!=null && entityList.size()>0){
			return entityList.get(0);
		}else{
			return null;
		}
		 
	}
	public FreightPriceAnalyseAlertConfigEntity getSpreadConfig(String flightNo,String flightDate,String contrastSourceKey,Long userId){
		String hql = "from FreightPriceAnalyseAlertConfigEntity f where f.flgDeleted = 0 ";
		List<Object> list = new ArrayList<Object>();
		
		if(StringUtils.isNotBlank(flightNo)){
			hql+=" and f.flightNo = ? ";
			list.add(flightNo);
		}
		if(flightDate != null ){
			/*Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			calendar.add(Calendar.DATE, 1);*/
			hql+=" and f.flightDate = ?  ";
			try {
				list.add(DateUtils.parseDate(flightDate,"yyyy-MM-dd"));
			} catch (ParseException e) {
				
				e.printStackTrace();
			}
			
		}
		if(StringAndDateAndFileUtil.checkString(contrastSourceKey)){
			hql+=" and f.contrastSourceKey = ? ";
			list.add(contrastSourceKey);
		}
		
		if(userId != null){
			hql+=" and f.creator = ? ";
			list.add(userId);
		}
		
		System.out.println("查询hql语句："+hql.toString());
		
			List<FreightPriceAnalyseAlertConfigEntity> entityList = this.find(hql,list.toArray(),FreightPriceAnalyseAlertConfigEntity.class);
		
		if(entityList!=null && entityList.size()>0){
			return entityList.get(0);
		}else{
			return null;
		}
	}
	
	public FreightPriceAnalyseAlertConfigEntity getContrastConfig(String flightNo,String contrastFlightNo,String flightDate,Long userId){
		String hql = "from FreightPriceAnalyseAlertConfigEntity f where f.flgDeleted = 0 ";
		List<Object> list = new ArrayList<Object>();
		
		if(StringUtils.isNotBlank(flightNo)){
			hql+=" and f.flightNo = ? ";
			list.add(flightNo);
		}
		
		if(StringUtils.isNotBlank(contrastFlightNo)){
			hql+=" and f.contrastFlightNo = ? ";
			list.add(contrastFlightNo);
		}
		
		if(flightDate != null ){
			/*Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			calendar.add(Calendar.DATE, 1);*/
			hql+=" and f.flightDate = ?  ";
			try {
				list.add(DateUtils.parseDate(flightDate,"yyyy-MM-dd"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
		
		if(userId != null){
			hql+=" and f.creator = ? ";
			list.add(userId);
		}
		List<FreightPriceAnalyseAlertConfigEntity> entityList = this.find(hql,list.toArray(),FreightPriceAnalyseAlertConfigEntity.class);
		
		if(entityList!=null && entityList.size()>0){
			return entityList.get(0);
		}else{
			return null;
		}
	}
}
